﻿// -----------------------------------------------------------------------
// <copyright file="PartWorkOrder_DAL.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace PMSGSC.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class PartWorkOrder_DAL:BaseDAL
    {
        // Lấy tất cả PartWorkOrder
        public static List<PartWorkOrder> GetAll()
        {
            DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, DB.PartWorkOrders);
            return DB.PartWorkOrders.ToList();
        }
        // Lấy PartWorkOrder theo Id
        public static PartWorkOrder GetDataById(PartWorkOrder jp)
        {
            return DB.PartWorkOrders.Where(m => m.Parts_ID == jp.Parts_ID && m.WorkOrder_ID == jp.WorkOrder_ID).FirstOrDefault();
        }
        //Thêm một PartWorkOrder
        public static bool Insert(PartWorkOrder PartWorkOrder)
        {
            try
            {
                DB.PartWorkOrders.InsertOnSubmit(PartWorkOrder);
                DB.SubmitChanges();
                return true;
            }
            catch(Exception ex)
            {
                return false;
            }

        }
        // Xóa một PartWorkOrder
        public static bool Delete(PartWorkOrder PartWorkOrder)
        {
            PartWorkOrder pw = GetDataById(PartWorkOrder);
            if (pw != null)
            {
                DB.PartWorkOrders.DeleteOnSubmit(pw);
                DB.SubmitChanges();
                return true;
            }
            return false;

        }
        public static bool DeleteByID(PartWorkOrder jp)
        {
            PartWorkOrder objPartWorkOrder = GetDataById(jp);
            return Delete(objPartWorkOrder);
        }
        // Cập nhật PartWorkOrder
        public static bool Update(PartWorkOrder PartWorkOrder)
        {
            try
            {
                //PartWorkOrder objPartWorkOrder = GetDataById(PartWorkOrder);
                //objPartWorkOrder.WorkOrder_ID = PartWorkOrder.WorkOrder_ID;
                //objPartWorkOrder.Parts_ID = PartWorkOrder.Parts_ID;
                //objPartWorkOrder.Quantity = PartWorkOrder.Quantity;
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
        public static bool isExits(PartWorkOrder jp)
        {
            PartWorkOrder objPartWorkOrder = null;
            objPartWorkOrder = DB.PartWorkOrders.Where(j => ((j.WorkOrder_ID == jp.WorkOrder_ID) &&
                (j.Parts_ID == jp.Parts_ID))).FirstOrDefault();
            return objPartWorkOrder != null ? true : false;
        }
        public static List<PartWorkOrder> GetDataByWorkOrderID(int id, bool ischanged)
        {
            if (ischanged)
            {
                DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, DB.PartWorkOrders);
            }
            return DB.PartWorkOrders.Where(j => j.WorkOrder_ID == id).ToList();
        }
    }
}
